package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.air.query.SupplierOutsideOrderAirDTO;
import com.ctshk.rpc.booking.dto.outside.air.query.SupplierOutsideOrderAirTouristDTO;
import com.ctshk.rpc.booking.dto.outside.air.query.SupplierOutsideOrderAirTouristTypeDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderAir;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderAirTourist;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderAirTouristType;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderAirMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderAirTouristMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderAirTouristTypeMapper;
import com.ctshk.rpc.booking.req.outside.air.save.SupplierOutsideOrderAirReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderAirService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外机票出行信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderAirServiceImpl implements ISupplierOutsideOrderAirService {
    @Autowired
    SupplierOutsideOrderAirMapper supplierOutsideOrderAirMapper;
    @Autowired
    SupplierOutsideOrderAirTouristMapper supplierOutsideOrderAirTouristMapper;
    @Autowired
    SupplierOutsideOrderAirTouristTypeMapper supplierOutsideOrderAirTouristTypeMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderAirReq> supplierOutsideOrderAirReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderAir> outsideOrderAirWrapper = new QueryWrapper<>();
            outsideOrderAirWrapper.lambda().eq(SupplierOutsideOrderAir::getOrderId, supplierOrderId);
            supplierOutsideOrderAirMapper.delete(outsideOrderAirWrapper);
            QueryWrapper<SupplierOutsideOrderAirTouristType> outsideOrderAirTouristTypeWrapper = new QueryWrapper<>();
            outsideOrderAirTouristTypeWrapper.lambda().eq(SupplierOutsideOrderAirTouristType::getOrderId, supplierOrderId);
            supplierOutsideOrderAirTouristTypeMapper.delete(outsideOrderAirTouristTypeWrapper);
            QueryWrapper<SupplierOutsideOrderAirTourist> outsideOrderAirTouristWrapper = new QueryWrapper<>();
            outsideOrderAirTouristWrapper.lambda().eq(SupplierOutsideOrderAirTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderAirTouristMapper.delete(outsideOrderAirTouristWrapper);
        }
        //新增飞机信息
        supplierOutsideOrderAirReqList.forEach(outsideOrderAirReq -> {
            SupplierOutsideOrderAir supplierOutsideOrderAir = EntityUtil.copy(outsideOrderAirReq, SupplierOutsideOrderAir.class);
            supplierOutsideOrderAir.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderAir.setOrderId(supplierOrderId);
            supplierOutsideOrderAir.setCreateId(userId);
            supplierOutsideOrderAir.setGmtCreate(nowDateTime);
            supplierOutsideOrderAir.setModifiedId(userId);
            supplierOutsideOrderAir.setGmtModified(nowDateTime);
            supplierOutsideOrderAir.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderAirMapper.insert(supplierOutsideOrderAir);
            if (CollectionUtils.isNotEmpty(outsideOrderAirReq.getSupplierOutsideOrderAirTouristTypeList())) {
                //新增旅客类型信息
                outsideOrderAirReq.getSupplierOutsideOrderAirTouristTypeList().forEach(outsideOrderAirTouristTypeReq -> {
                    SupplierOutsideOrderAirTouristType supplierOutsideOrderAirTouristType = EntityUtil.copy(outsideOrderAirTouristTypeReq, SupplierOutsideOrderAirTouristType.class);
                    supplierOutsideOrderAirTouristType.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderAirTouristType.setOrderId(supplierOrderId);
                    supplierOutsideOrderAirTouristType.setAirId(supplierOutsideOrderAir.getId());
                    supplierOutsideOrderAirTouristType.setCreateId(userId);
                    supplierOutsideOrderAirTouristType.setGmtCreate(nowDateTime);
                    supplierOutsideOrderAirTouristType.setModifiedId(userId);
                    supplierOutsideOrderAirTouristType.setGmtModified(nowDateTime);
                    supplierOutsideOrderAirTouristType.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderAirTouristTypeMapper.insert(supplierOutsideOrderAirTouristType);
                    if (CollectionUtils.isNotEmpty(outsideOrderAirTouristTypeReq.getSupplierOutsideOrderAirTouristList())) {
                        //新增旅客信息
                        outsideOrderAirTouristTypeReq.getSupplierOutsideOrderAirTouristList().forEach(outsideOrderAirTouristReq -> {
                            SupplierOutsideOrderAirTourist supplierOutsideOrderAirTourist = EntityUtil.copy(outsideOrderAirTouristReq, SupplierOutsideOrderAirTourist.class);
                            supplierOutsideOrderAirTourist.setId(SnowflakeIdWorker.nextId());
                            supplierOutsideOrderAirTourist.setOrderId(supplierOrderId);
                            supplierOutsideOrderAirTourist.setAirId(supplierOutsideOrderAir.getId());
                            supplierOutsideOrderAirTourist.setCreateId(userId);
                            supplierOutsideOrderAirTourist.setGmtCreate(nowDateTime);
                            supplierOutsideOrderAirTourist.setModifiedId(userId);
                            supplierOutsideOrderAirTourist.setGmtModified(nowDateTime);
                            supplierOutsideOrderAirTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                            supplierOutsideOrderAirTouristMapper.insert(supplierOutsideOrderAirTourist);
                        });
                    }
                });
            }
        });
    }

    @Override
    public List<SupplierOutsideOrderAirDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderAirDTO> supplierOutsideOrderAirDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderAir> airWrapper = new QueryWrapper();
        airWrapper.lambda().eq(SupplierOutsideOrderAir::getOrderId, id);
        List<SupplierOutsideOrderAir> supplierOutsideOrderAirs = supplierOutsideOrderAirMapper.selectList(airWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderAirs)) {
            supplierOutsideOrderAirDTOS = EntityUtil.copyList(supplierOutsideOrderAirs, SupplierOutsideOrderAirDTO.class);
            supplierOutsideOrderAirDTOS.forEach(air -> {
                //查询机票旅客类型列表
                QueryWrapper<SupplierOutsideOrderAirTouristType> airTouristTypeWrapper = new QueryWrapper();
                airTouristTypeWrapper.lambda().eq(SupplierOutsideOrderAirTouristType::getOrderId, id);
                List<SupplierOutsideOrderAirTouristType> supplierOutsideOrderAirTouristTypes = supplierOutsideOrderAirTouristTypeMapper.selectList(airTouristTypeWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderAirTouristTypes)) {
                    List<SupplierOutsideOrderAirTouristTypeDTO> supplierOutsideOrderAirTouristTypeDTOList = EntityUtil.copyList(supplierOutsideOrderAirTouristTypes, SupplierOutsideOrderAirTouristTypeDTO.class);
                    air.setSupplierOutsideOrderAirTouristTypeDTOList(supplierOutsideOrderAirTouristTypeDTOList);
                    supplierOutsideOrderAirTouristTypeDTOList.forEach(airTouristType -> {
                        //查询机票旅客列表
                        QueryWrapper<SupplierOutsideOrderAirTourist> airTouristWrapper = new QueryWrapper();
                        airTouristWrapper.lambda().eq(SupplierOutsideOrderAirTourist::getOrderId, id);
                        List<SupplierOutsideOrderAirTourist> supplierOutsideOrderAirTourists = supplierOutsideOrderAirTouristMapper.selectList(airTouristWrapper);
                        if (CollectionUtils.isNotEmpty(supplierOutsideOrderAirTourists)) {
                            List<SupplierOutsideOrderAirTouristDTO> supplierOutsideOrderAirTouristDTOList = EntityUtil.copyList(supplierOutsideOrderAirTourists, SupplierOutsideOrderAirTouristDTO.class);
                            airTouristType.setSupplierOutsideOrderAirTouristDTOList(supplierOutsideOrderAirTouristDTOList);
                        }
                    });
                }
            });
        }
        return supplierOutsideOrderAirDTOS;
    }
}
